/*
 * GUIPrincipal.java
 *
 * Created on 25 de Outubro de 2006, 18:47
 */

package plotagrafico;

import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 *
 * @author  Gustavo
 */
public class GUIPrincipal extends javax.swing.JFrame {
    
    /** Creates new form GUIPrincipal */
    public GUIPrincipal() throws IOException {
        initComponents();
        
        this.setLocationRelativeTo(null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        painelNos = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jSpinner1 = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Plotador de n\u00f3s");
        setResizable(false);
        painelNos.setBackground(new java.awt.Color(255, 255, 255));
        painelNos.setPreferredSize(new java.awt.Dimension(900, 180));
        org.jdesktop.layout.GroupLayout painelNosLayout = new org.jdesktop.layout.GroupLayout(painelNos);
        painelNos.setLayout(painelNosLayout);
        painelNosLayout.setHorizontalGroup(
            painelNosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 900, Short.MAX_VALUE)
        );
        painelNosLayout.setVerticalGroup(
            painelNosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 180, Short.MAX_VALUE)
        );

        jButton1.setText("Plotar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(painelNos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(802, Short.MAX_VALUE)
                .add(jSpinner1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(painelNos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jButton1)
                    .add(jSpinner1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            this.pintaNos();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /**
    throws IOException  * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUIPrincipal gp = new GUIPrincipal();
                    gp.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    private void pintaNos() throws IOException {
        
        Graphics g = this.painelNos.getGraphics();
        
        int arquivo = (Integer)this.jSpinner1.getValue();
        File f = new File("./entrada/PlotaGrafico_" + arquivo + ".txt");
        BufferedReader in = new BufferedReader(new FileReader(f));
        
        g.setColor(Color.white);
        g.fillRect(0, 0 , 900, 180);
        
        String linha;
        while ((linha = in.readLine()) != null) {
            
            if (linha.startsWith("Node")) {
                
                StringTokenizer st = new StringTokenizer(linha, "( ,");
                
                st.nextToken();
                String numNo = st.nextToken();
                
                int x = (int)Double.parseDouble(st.nextToken());
                int y = (int)Double.parseDouble(st.nextToken());
                
                g.setColor(Color.black);
                g.drawString(numNo, this.transforma(x), this.transforma(y));
                
                g.setColor(Color.red);
                g.drawOval(this.transforma(x) - this.transforma(250), this.transforma(y) - this.transforma(250), this.transforma(500), this.transforma(500));
            }
            
            
        }        
    }    
    
    private int transforma(int x) {
        
        return (900 * x) / 1500;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JPanel painelNos;
    // End of variables declaration//GEN-END:variables
    
}
